<?php
    class Quanlytrangthaiduan extends CI_Controller {
        function __construct() {
            parent::__construct();
            $this->load->helper('url');
            $this->load->library(array('form_validation','session'));
            $this->load->library('adminlayout');
            $this->adminlayout->setLayout("admin/layout");
            $this->load->model('admin/duan/quanlytrangthaiduan_model');
            $userSession = $this->session->userdata('user');
            // ki?m tra xem dang nh?p chua n?u chua thì v? tr? v? trang dang nh?p
            if($userSession['username'] == false){
                redirect(base_url('/index/login?returnUrl=').urlencode(RETURN_URL));
            
            }else{ 
                //n?u dang nh?p r?i mà ko ph?i là admin thì tr? v? trang home (trang này là trang c?a ph?n member nên t? 
                //chua vi?t t? ch? link t?i trang dó thôi)
                if($userSession['useradmin'] != 1){
                    redirect(base_url('nhanvien/event/list_event'));
                }
            }
        }
        
        function listTrangThai(){
            $title = "Danh sách trạng thái dự án";
            $this->adminlayout->view('/admin/duan/danhsachtrangthai_view', null,$title);    
        }
        
        function hienThiDanhSachTrangThai() {
            $result = $this->quanlytrangthaiduan_model->getDanhSachTrangThai();
            
            //return result to jTable
            $jTableResult = array();
    		$jTableResult['Result'] = "OK";
            $jTableResult['Records'] = $result;
            print json_encode($jTableResult);
        }
        
        function suaTrangThai() {
            $jTableResult = array();
            if(!empty($_POST['TenTrangThai'])){
                $data['MaTrangThai'] = $_POST['MaTrangThai'];
                $data['TenTrangThai'] = $_POST['TenTrangThai'];
                $this->quanlytrangthaiduan_model->suaTrangThai($data);
                $jTableResult['Result'] = "OK";
                print json_encode($jTableResult);
            }else{
                $jTableResult['Result'] = "ERROR";
                $jTableResult['Message'] = "Nhập Tên Trạng Thái";
                print json_encode($jTableResult);
            }
        }
        
        function themTrangThai() {
            $jTableResult = array();
            if(!empty($_POST['TenTrangThai'])){
                $data['TenTrangThai'] = $_POST['TenTrangThai'];
                $result = $this->quanlytrangthaiduan_model->themTrangThai($data);
                $jTableResult['Result'] = "OK";
        		$jTableResult['Record'] = $result;
        		print json_encode($jTableResult);
            }else{
                $jTableResult['Result'] = "ERROR";
                $jTableResult['Message'] = "Nhập Tên Trạng Thái";
                print json_encode($jTableResult);
            }
        }
        
        function xoaTrangThai() {
            try{
                if(isset($_POST["MaTrangThai"])){
                    $idMaTrangThai = $_POST["MaTrangThai"];
                    $this->quanlytrangthaiduan_model->xoaTrangThai($idMaTrangThai);
                    $jTableResult = array();
                    $jTableResult['Result'] = "OK";
                    print json_encode($jTableResult);
                    
                }
                }catch(Exception $ex){
                    $jTableResult = array();
        	        $jTableResult['Result'] = "ERROR";
    	            $jTableResult['Message'] = $ex->message();
        	        print json_encode($jTableResult);
                }
        }
    }

?>